package com.threeti.jiayusms.infrastructure.communicationfunction;

import static org.apache.commons.lang.StringUtils.isNotEmpty;
import static org.hibernate.criterion.Restrictions.disjunction;
import static org.hibernate.criterion.Restrictions.eq;
import static org.hibernate.criterion.Restrictions.like;
import static org.springframework.util.CollectionUtils.isEmpty;

import java.io.UnsupportedEncodingException;
import java.util.List;

import org.hibernate.SessionFactory;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Junction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.threeti.jiayusms.domain.model.CommunicationFunction;
import com.threeti.jiayusms.framework.dao.AbstractCustomKeyDao;
import com.threeti.jiayusms.framework.util.DynaSearchCriteria;
import com.threeti.jiayusms.framework.util.PagedListHandler;
import com.threeti.jiayusms.interfaces.FunctionTemplateDynaCriteriaDto;

@Repository
public class CommunicationFunctionDaoImpl extends AbstractCustomKeyDao<String, CommunicationFunction> implements CommunicationFunctionDao {
	@Autowired
	public CommunicationFunctionDaoImpl(SessionFactory sessionFactory) {
		super(sessionFactory);
	}

	@Override
	protected DetachedCriteria buildDefaultCriteria() {
		return DetachedCriteria.forClass(CommunicationFunction.class);
	}
	
	@Override
	public PagedListHandler<CommunicationFunction> findAllFunctionTemplatesByPage(int currentPageIndex, int startingIndex) {
    PagedListHandler<CommunicationFunction> pagedListHandler = this.findAllByPage(currentPageIndex, startingIndex);
    List<CommunicationFunction> pagedList = pagedListHandler.getPageList();
    if (!isEmpty(pagedList))  return pagedListHandler;

    return PagedListHandler.EMPTY_LIST;
	}

	@Override
	public PagedListHandler<CommunicationFunction> findFunctionTemplatesByPage(DynaSearchCriteria dynaSearchCriteria,
			int currentPageIndex, int startingIndex) {
		DetachedCriteria criteria = buildDefaultCriteria();
		FunctionTemplateDynaCriteriaDto dynaCriteria = (FunctionTemplateDynaCriteriaDto)dynaSearchCriteria;
		
		//精确条件查询
		String functionType = dynaCriteria.getFunctionType();
		if (isNotEmpty(functionType)){
			criteria.add(eq("this.functionType", functionType));			
		}
		
		String relationTable = dynaCriteria.getRelationTable();
		if (isNotEmpty(relationTable)){
			criteria.add(eq("this.relationTable", relationTable));
		}
		
		String relationField = dynaCriteria.getRelationField();
		if (isNotEmpty(relationField)){
			criteria.add(eq("this.relationField", relationField));
		}
		
		String associatedField = dynaCriteria.getAssociatedField();
		if (isNotEmpty(associatedField)){
			criteria.add(eq("this.associatedField", associatedField));
		}
		
		String functionContent = dynaCriteria.getFunctionContent();
		if (isNotEmpty(functionContent)){
			criteria.add(eq("this.functionContent", functionContent));
		}
		
		//模糊查询
		String templateId = dynaCriteria.getFunctionTemplateId();
		if(isNotEmpty(templateId)){
			String[] keywords = templateId.split(" ");
			Junction orClause = disjunction();
			for (int i = 0; i < keywords.length; i++) {
				String keyword = "%" + keywords[i] + "%";
				orClause.add(like("this.customId", keyword).ignoreCase());
			}
			criteria.add(orClause);
		}
		
		String functionName = dynaCriteria.getFunctionName();
		try {
		if(isNotEmpty(functionName)){
			String[] keywords = functionName.split(" ");
			Junction orClause = disjunction();
			for (int i = 0; i < keywords.length; i++) {
				String encodeKeyWord = new String(keywords[i].getBytes("iso8859-1"), "utf-8");
//					String encodeKeyWord1 = new String(keywords[i].getBytes("gbk"), "utf-8");
//					String encodeKeyWord2 = new String(keywords[i].getBytes("gbk"), "gbk");
//					String encodeKeyWord3 = new String(keywords[i].getBytes("utf-8"), "utf-8");
//					String encodeKeyWord4 = new String(keywords[i].getBytes("utf-8"), "gbk");
//					String encodeKeyWord6 = new String(keywords[i].getBytes("iso8859-1"), "gbk");
//					System.out.println(encodeKeyWord1);
//					System.out.println(encodeKeyWord2);
//					System.out.println(encodeKeyWord3);
//					System.out.println(encodeKeyWord4);
//					System.out.println(encodeKeyWord5);
//					System.out.println(encodeKeyWord6);
				String keyword = "%" + encodeKeyWord + "%";
				orClause.add(like("this.functionName", keyword).ignoreCase());
			}
			criteria.add(orClause);
		}
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}

		PagedListHandler<CommunicationFunction>  pagedListHandler = this.findByPage(criteria, currentPageIndex, startingIndex);
    List<CommunicationFunction> pagedList = pagedListHandler.getPageList();
    if (!isEmpty(pagedList))  return pagedListHandler;

    return PagedListHandler.EMPTY_LIST;
	}
	
}
